from machine import Pin, I2S
import time
from config import SCK,WS,SD
def record_audio():
   # 录音相关设置
   sck = Pin(SCK)
   ws = Pin(WS)
   sd = Pin(SD)
   audio_in = I2S(0,
                  sck=sck, ws=ws, sd=sd,
                  mode=I2S.RX,
                  bits=16,
                  format=I2S.MONO,
                  rate=8000,
                  ibuf=32768)

   chunk_size = 512
   chunks = []
   print('开始录音')
   start_time = time.ticks_ms()
   while time.ticks_ms() - start_time < 4000:
       ibuf = bytearray(chunk_size)
       num_read = audio_in.readinto(ibuf)
       chunks.append(ibuf)
   audio_in.deinit()
   return chunks



